package com.example.submit;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 根据sessionKey去判断
 * 让用户不能疯狂点击按钮提交
 * @author 09948
 */
@Component
public class RepeatedSubmitUtil {

    @Resource
    private RedisUtil redisUtil;

    public boolean lock(String sessionKey) throws Exception {
        Boolean aBoolean = redisUtil.strSetIfAbsent(sessionKey, UUID.randomUUID().toString());
        if (aBoolean) {
            redisUtil.setExpire(sessionKey, 60L);
            return true;
        } else {
            throw new Exception("请不要操作太快啦");
        }
    }

    public void unlock(String sessionKey) {
        redisUtil.del(sessionKey);
    }

}
